#include "modbusrtu.h"
#include "QtSerialPort"
#include <QMessageBox>
#include <QtGlobal>
#include <QThread>

// CRC16_MODBUS
// CRC 高位字节值表
static const quint8 auchCRCHi[] = {
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
} ;
// CRC 低位字节值表
static const quint8 auchCRCLo[] = {
    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
    0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
    0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
    0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
    0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
    0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
    0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
    0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
    0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
    0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
    0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
    0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
    0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
    0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
    0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
    0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
    0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
    0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
    0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
    0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
    0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
    0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
    0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
    0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};

ModbusRtu::ModbusRtu()
{
  //comPort = new QSerialPort();
// connect(this,SIGNAL(readyRead()),this,SLOT(revData()));
}



void ModbusRtu::iniSerialPort(QVariant sBaudRate,QVariant sDataBits,QVariant sParity,QVariant sStopBits)
{

    this->setBaudRate(sBaudRate.value<QSerialPort::BaudRate> ());
    this->setDataBits(sDataBits.value<QSerialPort::DataBits> ());
    this->setParity(sParity.value<QSerialPort::Parity> ());
    this->setStopBits(sStopBits.value<QSerialPort::StopBits> ());
    this->setFlowControl(QSerialPort::NoFlowControl);

//        this->setBaudRate(QSerialPort::Baud9600);
//        this->setDataBits(QSerialPort::Data8);
//        this->setParity(QSerialPort::NoParity);
//        this->setStopBits(QSerialPort::OneStop);
//        this->setFlowControl(QSerialPort::NoFlowControl);


 //   connect(this,SIGNAL(readyRead()),this,SLOT(revData()));
}


/*!
 * @file verifyCRC16
 * @brief 对于文件的简单注释
 * @note 关闭串口
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
void ModbusRtu::portClose(QSerialPort *port)
{
       port->close();
       delete port;
}

/*!
 * @file verifyCRC16
 * @brief 对于文件的简单注释
 * @note 打开串口
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::portOpen(QString portString)
{
    this->setPortName(portString);
    if (this->open(QIODevice::ReadWrite))
    {
        this->setDataTerminalReady(false);
        this->setRequestToSend(false);

        return  1;
    } else {
        return 0;
        QMessageBox::information(NULL,"错误","打开失败",0,0);
    }
}

/*!
 * @file verifyCRC16
 * @brief 对于文件的简单注释
 * @note 查找串口
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
QStringList ModbusRtu::searchPort()
{
    QStringList portList;
    portList.clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        portList << info.portName() + " " + info.description();
    }
    return  portList;
}


/*!
 * @file verifyCRC16
 * @brief 对于文件的简单注释
 * @note 计算CRC校验
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
QByteArray ModbusRtu::verifyCRC16(QByteArray buf,qint32 Len)
{
        buf.resize(Len);
        QByteArray a;

        quint8 uchCRCHi = 0xFF;
        quint8 uchCRCLo = 0xFF;
        quint16 uIndex;
        foreach (quint8 c, buf){
            uIndex = uchCRCLo ^ c;
            uchCRCLo = uchCRCHi^auchCRCHi[uIndex];
            uchCRCHi = auchCRCLo[uIndex];
        }
        a.resize(2);
        a[0] = uchCRCLo;
        a[1] = uchCRCHi;
        return a;

}




/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 读取输出线圈 功能码 01H
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::ReadOutputStatus(int iDevAdd, int iAddress, int iLength)
{
    QByteArray VerifyCRC16;
    VerifyCRC16.resize(2);
    QByteArray SendCommandByte;
    SendCommandByte[0] = (uchar)iDevAdd;
    SendCommandByte[1] = 0x01;
    SendCommandByte[2] = (char)(iAddress/256);
    SendCommandByte[3] = (char)(iAddress%256);
    SendCommandByte[4] = (char)(iLength/256);
    SendCommandByte[5] = (char)(iLength%256);
    VerifyCRC16 = verifyCRC16(SendCommandByte,6);
    SendCommandByte[6] = VerifyCRC16[0];
    SendCommandByte[7] = VerifyCRC16[1];

    return  sendData(SendCommandByte);

}

QByteArray ModbusRtu::revReadOutputStatus(QByteArray &revDataRead)
{
    int byteLength;
    int iLength;

    iLength = SendCommandByte[4]<<8 + SendCommandByte[5];

    if(iLength%8 == 0)
    {
        byteLength = iLength/8;
    }else{
        byteLength = iLength/8 + 1;
    }
    if((char)SendCommandByte[1] == 0x01 && (char)revDataRead[1] == 0x01 && (char)revDataRead[2] == byteLength)
    {
        return getByteArray(revDataRead,3,byteLength);
    }else{
        return NULL;
    }

}



/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 读取输入线圈 功能码 02H
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::ReadInputStatus(int iDevAdd, int iAddress, int iLength)
{
    QByteArray VerifyCRC16;
    VerifyCRC16.resize(2);
    QByteArray SendCommandByte;
    SendCommandByte[0] = (uchar)iDevAdd;
    SendCommandByte[1] = 0x02;
    SendCommandByte[2] = (char)(iAddress/256);
    SendCommandByte[3] = (char)(iAddress%256);
    SendCommandByte[4] = (char)(iLength/256);
    SendCommandByte[5] = (char)(iLength%256);
    VerifyCRC16 = verifyCRC16(SendCommandByte,6);
    SendCommandByte[6] = VerifyCRC16[0];
    SendCommandByte[7] = VerifyCRC16[1];

    return sendData(SendCommandByte);
}


QByteArray ModbusRtu::revReadInputStatus(QByteArray &revDataRead)
{
    int byteLength;
    int iLength;

    iLength = SendCommandByte[4]<<8 + SendCommandByte[5];

    if(iLength%8 == 0)
    {
        byteLength = iLength/8;
    }else{
        byteLength = iLength/8 + 1;
    }
    if((char)SendCommandByte[1] == 0x02 && (char)revDataRead[1] == 0x02 && (char)revDataRead[2] == byteLength)
    {
        return getByteArray(revDataRead,3,byteLength);
    }else{
        return NULL;
    }
}




/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 读取输入寄存器 功能码 0x04
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::ReadInputReg(int iDevAdd, int iAddress, int iLength)
{
    QByteArray VerifyCRC16;
    VerifyCRC16.resize(2);
    QByteArray SendCommandByte;
    SendCommandByte[0] = (uchar)iDevAdd;
    SendCommandByte[1] = 0x04;
    SendCommandByte[2] = (char)(iAddress/256);
    SendCommandByte[3] = (char)(iAddress%256);
    SendCommandByte[4] = (char)(iLength/256);
    SendCommandByte[5] = (char)(iLength%256);
    VerifyCRC16 = verifyCRC16(SendCommandByte,6);
    SendCommandByte[6] = VerifyCRC16[0];
    SendCommandByte[7] = VerifyCRC16[1];

    return sendData(SendCommandByte);

}

QByteArray ModbusRtu::revReadInputReg(QByteArray &revDataRead)
{
    int byteLength;
    int iLength;

    iLength = SendCommandByte[4]<<8 + SendCommandByte[5];

    byteLength = iLength*2;
    if((char)SendCommandByte[1] == 0x04 && (char)revDataRead[1] == 0x04 && (char)revDataRead[2] == byteLength)
    {
        return getByteArray(revDataRead,3,byteLength);
    }else{
        return NULL;
    }
}


/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 强制单线圈 功能码0x05
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::ForceCoil(int iDevAdd, int iAddress, bool SetValue)
{
    QByteArray VerifyCRC16;
    VerifyCRC16.resize(2);

    SendCommandByte[0] = (uchar)iDevAdd;
    SendCommandByte[1] = 0x05;
    SendCommandByte[2] = (char)(iAddress/256);
    SendCommandByte[3] = (char)(iAddress%256);
    if(SetValue)
    {
        SendCommandByte[4] = 0xFF;
        SendCommandByte[5] = 0X00;
    }else{
        SendCommandByte[4] = 0x00;
        SendCommandByte[5] = 0x00;
    }
    VerifyCRC16 = verifyCRC16(SendCommandByte,6);
    SendCommandByte[6] = VerifyCRC16[0];
    SendCommandByte[7] = VerifyCRC16[1];
    return sendData(SendCommandByte);
}

bool ModbusRtu::revForceCoil(QByteArray &revDataRead)
{
    if((char)SendCommandByte[1] == 0x05)
    {
        return ArraryEquals(SendCommandByte,revDataRead);
    }else{
        return false;
    }

}

/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 读取保持性继电器 03
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::ReadKeepReg(int iDevAdd, int iAddress, int iLength)
{

    QByteArray VerifyCRC16;
    VerifyCRC16.resize(2);

    QByteArray SendCommandByte;
    SendCommandByte.resize(8);
    SendCommandByte[0] = (uchar)(iDevAdd);
    SendCommandByte[1] = 0x03;
    SendCommandByte[2] = (uchar)((iAddress-iAddress%256)/256);
    SendCommandByte[3] = (uchar)(iAddress%256);
    SendCommandByte[4] = (uchar)((iLength-iLength%256)/256);
    SendCommandByte[5] = (uchar)(iLength%256);
    VerifyCRC16 = verifyCRC16(SendCommandByte,6);
    SendCommandByte[6] = VerifyCRC16[0];
    SendCommandByte[7] = VerifyCRC16[1];

    return sendData(SendCommandByte);
}

QByteArray ModbusRtu::revReadKeepReg(QByteArray &revDataRead)
{
    int byteLength;
    int iLength;

    iLength = SendCommandByte[4]<<8 + SendCommandByte[5];

    if(iLength%8 == 0)
    {
        byteLength = iLength/8;
    }else{
        byteLength = iLength/8 + 1;
    }
    if((char)SendCommandByte[1] == 0x03 && (char)revDataRead[1] == 0x03 && (char)revDataRead[2] == byteLength)
    {
        return getByteArray(revDataRead,3,byteLength);
    }else{
        return NULL;
    }
}

/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 预置单字保持型寄存器 功能码0x06
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */

bool ModbusRtu::PreSetSingleReg(quint32 iDevAdd, quint32 iAddress, short SetValue)
{
    QByteArray VerifyCRC16;
    VerifyCRC16.resize(2);

    QByteArray a = GetByteArrayFrom16Bit(SetValue);

    QByteArray SendCommandByte;
    // SendCommandByte.resize(8);
    SendCommandByte[0] = (uchar)(iDevAdd);
    SendCommandByte[1] = 0x06;
    SendCommandByte[2] = (uchar)(iAddress/256);
    SendCommandByte[3] = (uchar)(iAddress%256);
    SendCommandByte.append(GetByteArrayFrom16Bit(SetValue));
    VerifyCRC16 = verifyCRC16(SendCommandByte,6);
    SendCommandByte[6] = VerifyCRC16[0];
    SendCommandByte[7] = VerifyCRC16[1];

    return  sendData(SendCommandByte);
}

bool ModbusRtu::revPreSetSingleReg(QByteArray &revDataRead)
{
    if((char)SendCommandByte[1] == 0x06 && (char)revDataRead[1] == 0x06){
        return ArraryEquals(SendCommandByte,revDataRead);
    }else{
        return false;
    }
}

/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 预置多个寄存器 功能码10H
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */

bool ModbusRtu::PreSetMultiByteArray(qint16 iDevAdd, int iAddress, QByteArray SetValue)
{
    if(SetValue.isNull() || SetValue.length() == 0 || SetValue.length()%2 == 1)
    {
        QMessageBox::information(NULL,"错误","设置值错误",0,0);
        return 0;

    }

    quint32 RegLength = SetValue.length()/2;

    QByteArray VerifyCRC16;
    VerifyCRC16.resize(2);

    QByteArray SendCommandByte;
    SendCommandByte[0] = (uchar)iDevAdd;
    SendCommandByte[1] = 0x10;
    SendCommandByte[2] = (uchar)iAddress/256;
    SendCommandByte[3] = (uchar)iAddress%256;
    SendCommandByte[4] = (uchar)RegLength/256;
    SendCommandByte[5] = (uchar)RegLength%256;
    SendCommandByte[6] = (uchar)SetValue.length();
    SendCommandByte.append(SetValue);
    VerifyCRC16 = verifyCRC16(SendCommandByte,7+SetValue.length());

    SendCommandByte[7+SetValue.length()] = VerifyCRC16[0];
    SendCommandByte[7+1+SetValue.length()] = VerifyCRC16[1];

    return sendData(SendCommandByte);

}

bool ModbusRtu::revPreSetMultiByteArray(QByteArray &revDataRead)
{
    if((char)SendCommandByte[1] == 0x10 && (char)revDataRead[1] == 0x10){
        QByteArray b = getByteArray(revDataRead,0,6);
        QByteArray crc = verifyCRC16(b,6);
        return ArraryEquals(getByteArray(SendCommandByte,0,6),b) && crc[0] == revDataRead[6] && crc[1] == revDataRead[7];
    }else{
        return false;
    }
}



//QByteArray ModbusRtu::PreSetMultiReg(int iDevAdd, int iAddress, float SetValue)
//{
//    return PreSetMultiByteArray(iDevAdd,iAddress,GetByteArrayFrom32Bit(SetValue));
//}

//QByteArray ModbusRtu::PreSetMultiReg(int iDevAdd, int iAddress, int SetValue)
//{
//    return  PreSetMultiByteArray(iDevAdd,iAddress,GetByteArrayFrom32Bit(SetValue));
//}

//QByteArray ModbusRtu::PreSetMultiReg(int iDevAdd, int iAddress, uint SetValue)
//{

//}


/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 写入多个浮点数
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::PresetMultiRes(int iDevAdd, int iAddress, const QVector<float>& SetValue)
{
    QByteArray bSetValue ;
    foreach(const float& c, SetValue)
    {
        bSetValue.append(GetByteArrayFrom32Bit(c));
    }
    return PreSetMultiByteArray(iDevAdd,iAddress,bSetValue);
}

/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 写入多个整数
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::PresetMultiRes(int iDevAdd, int iAddress, const QVector<int>& SetValue)
{

    QByteArray bSetValue ;
    foreach(const int& c, SetValue)
    {
        bSetValue.append(GetByteArrayFrom32Bit(c));
    }
    return PreSetMultiByteArray(iDevAdd,iAddress,bSetValue);
}


/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 写入多个无符号整数
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::PresetMultiRes(int iDevAdd, int iAddress, const QVector<uint>& SetValue)
{
    QByteArray bSetValue ;
    foreach(const uint& c, SetValue)
    {
        bSetValue.append(GetByteArrayFrom32Bit(c));
    }
    return PreSetMultiByteArray(iDevAdd,iAddress,bSetValue);
}



/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 浮点数转换32位字节数组
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
QByteArray ModbusRtu::GetByteArrayFrom32Bit(float SetValue)
{
    QByteArray byteArr;
    char* dataChar = (char*)&SetValue;
    for(int index = 3; index >= 0; index--)
    {
        byteArr.append(dataChar[index]);
    }


    QByteArray res;
    res.resize(4);

    switch (setDataformat) {
    case DataFormat::DCBA:
        res[0] = byteArr[3];
        res[1] = byteArr[2];
        res[2] = byteArr[1];
        res[3] = byteArr[0];
        break;

    case DataFormat::BADC :
        res[0] = byteArr[1];
        res[1] = byteArr[0];
        res[2] = byteArr[3];
        res[3] = byteArr[2];
        break;
    case DataFormat::CDAB :
        res[0] = byteArr[2];
        res[1] = byteArr[3];
        res[2] = byteArr[0];
        res[3] = byteArr[1];
        break;

    case DataFormat::ABCD :
        res = byteArr;
        break;
    }
    return res;
}


/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 有符号整形转换32位字节数组
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
QByteArray ModbusRtu::GetByteArrayFrom32Bit(int SetValue)
{
    QByteArray byteArr;
    char* dataChar = (char*)&SetValue;
    for(int index = 3; index >= 0; index--)
    {
        byteArr.append(dataChar[index]);
    }


    QByteArray res;
    res.resize(4);

    switch (setDataformat) {
    case DataFormat::DCBA:
        res[0] = byteArr[3];
        res[1] = byteArr[2];
        res[2] = byteArr[1];
        res[3] = byteArr[0];
        break;

    case DataFormat::BADC :
        res[0] = byteArr[1];
        res[1] = byteArr[0];
        res[2] = byteArr[3];
        res[3] = byteArr[2];
        break;
    case DataFormat::CDAB :
        res[0] = byteArr[2];
        res[1] = byteArr[3];
        res[2] = byteArr[0];
        res[3] = byteArr[1];
        break;

    case DataFormat::ABCD :
        res = byteArr;
        break;
    }
    return res;
}


/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 无符号整型转换32位字节数组
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
QByteArray ModbusRtu::GetByteArrayFrom32Bit(uint SetValue)
{
    QByteArray byteArr;
    char* dataChar = (char*)&SetValue;
    for(int index = 3; index >= 0; index--)
    {
        byteArr.append(dataChar[index]);
    }


    QByteArray res;
    res.resize(4);

    switch (setDataformat) {
    case DataFormat::DCBA:
        res[0] = byteArr[3];
        res[1] = byteArr[2];
        res[2] = byteArr[1];
        res[3] = byteArr[0];
        break;

    case DataFormat::BADC :
        res[0] = byteArr[1];
        res[1] = byteArr[0];
        res[2] = byteArr[3];
        res[3] = byteArr[2];
        break;
    case DataFormat::CDAB :
        res[0] = byteArr[2];
        res[1] = byteArr[3];
        res[2] = byteArr[0];
        res[3] = byteArr[1];
        break;

    case DataFormat::ABCD :
        res = byteArr;
        break;
    }
    return res;
}

QByteArray ModbusRtu::GetByteArrayFrom16Bit(ushort SetValue)
{
    QByteArray byteArr;
    char* dataChar = (char*)&SetValue;
    for(int index = 1; index >= 0; index--)
    {
        byteArr.append(dataChar[index]);
    }
    QByteArray res;
    res.resize(2);

    switch (setDataformat) {
    case DataFormat::DCBA:
        res = byteArr;
        break;

    case DataFormat::BADC :
        break;
    case DataFormat::CDAB :
        res[0] = byteArr[1];
        res[1] = byteArr[0];
        break;

    case DataFormat::ABCD :

        break;
    }
    return res;
}




/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 比较两个数组是否相等
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::ArraryEquals(QByteArray b1, QByteArray b2)
{
    if(b1.isEmpty()||b2.isEmpty()) return false;
    if(b1.length() != b2.length()) return false;
    for(int i = 0; i < b1.length(); i++)
    {
        if(b1[i] != b2[i])
        {
            return false;
        }
    }
    return true;
}

/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 截取数组的个数
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
QByteArray ModbusRtu::getByteArray(QByteArray dest, int offset, int count)
{
    QByteArray res;
    res.resize(count);
    if(!dest.isEmpty() && dest.length() >= offset + count)
    {
        for(int i = 0;i < count;i++)
        {
            res[i] = dest[offset+i];
        }
        return res;
    }else{
        return NULL;
    }
}




/*!
 * @file filename
 * @brief 对于文件的简单注释
 * @note 发送接收数据
 *
 * @version
 * @date
 * @author  Gu
 * @copyright XXX有限公司
 */
bool ModbusRtu::sendData(QByteArray sendByteArry)
{
    qint64 result;
    result =  this->write(sendByteArry);
    if(result == sendByteArry.length() )
    {
        return true;
    }else {
        QMessageBox::information(NULL,"错误","发送或接收失败",0,0);
        return false;
    }


};
